<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge" >
  <title>JS实现倒计时 | 默默地向前端靠拢</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="很久之前就写了一个时间插件，该插件是根据工作开发过程中的需要写的，关于时间插件，应该有不少人开发过，这里我写了一个个人觉得还算可以的方法吧，不过代码有待优化，一旦功能写好了，除非有bug，不然我也就懒得去优化了。我写了一个倒计时的对象setCountDown.">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="JS实现倒计时">
<meta property="og:url" content="http://yoursite.com/2016/04/22/JS实现倒计时/index.html">
<meta property="og:site_name" content="默默地向前端靠拢">
<meta property="og:description" content="很久之前就写了一个时间插件，该插件是根据工作开发过程中的需要写的，关于时间插件，应该有不少人开发过，这里我写了一个个人觉得还算可以的方法吧，不过代码有待优化，一旦功能写好了，除非有bug，不然我也就懒得去优化了。我写了一个倒计时的对象setCountDown.">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-02-13T08:52:43.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JS实现倒计时">
<meta name="twitter:description" content="很久之前就写了一个时间插件，该插件是根据工作开发过程中的需要写的，关于时间插件，应该有不少人开发过，这里我写了一个个人觉得还算可以的方法吧，不过代码有待优化，一旦功能写好了，除非有bug，不然我也就懒得去优化了。我写了一个倒计时的对象setCountDown.">
  
    <link rel="alternative" href="/atom.xml" title="默默地向前端靠拢" type="application/atom+xml">
  
  
    <link rel="icon" href="/img/head.jpg">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="/img/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">心情</a></h1>
		</hgroup>

		
		<p class="header-subtitle">你所浪费的今天，是昨天死去的人奢望的明天；你所厌恶的现在，是未来的你回不去的曾经。</p>
		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
						<div class="icon-wrap icon-me hide" data-idx="3">
							<div class="user"></div>
							<div class="shoulder"></div>
						</div>
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
						<li>关于我</li>
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/">主页</a></li>
				        
							<li><a href="/archives">所有文章</a></li>
				        
							<li><a href="/categories/随笔">随笔总结</a></li>
				        
							<li><a href="/categories/笔记">学习笔记</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
								<a class="github" target="_blank" href="http://springxiege.gitbub.io" title="github">github</a>
					        
								<a class="weibo" target="_blank" href="http://weibo.com/springxiegel" title="weibo">weibo</a>
					        
								<a class="mail" target="_blank" href="mailto://springxiege@sina.cn" title="mail">mail</a>
					        
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						<a href="/tags/Git/" style="font-size: 10px;">Git</a> <a href="/tags/HTML5/" style="font-size: 10px;">HTML5</a> <a href="/tags/JS/" style="font-size: 20px;">JS</a> <a href="/tags/JavaScript/" style="font-size: 13.33px;">JavaScript</a> <a href="/tags/Node/" style="font-size: 10px;">Node</a> <a href="/tags/NodeJS/" style="font-size: 16.67px;">NodeJS</a> <a href="/tags/NodeJs/" style="font-size: 10px;">NodeJs</a> <a href="/tags/React-js/" style="font-size: 13.33px;">React.js</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a>
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.52cik.com/">乱码大大的博客</a>
			        
			          <a target="_blank" class="main-nav-link switch-friends-link" href="http://webcaiqingchong.github.io/">菜青虫的博客</a>
			        
			        </div>
				</section>
				

				
				
				<section class="switch-part switch-part4">
				
					<div id="js-aboutme">我是心情，我是一名前端工作者，目前靠Coding讨生活！平时喜欢玩三国杀，听音乐，运动也喜欢，只是现在运动项目做得少了。</div>
				</section>
				
			</div>
		</div>
	</header>				
</div>

    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">心情</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
			
				<img lazy-src="/img/head.jpg" class="js-avatar">
			
			</div>
			<hgroup>
			  <h1 class="header-author">心情</h1>
			</hgroup>
			
			<p class="header-subtitle">你所浪费的今天，是昨天死去的人奢望的明天；你所厌恶的现在，是未来的你回不去的曾经。</p>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/">主页</a></li>
		        
					<li><a href="/archives">所有文章</a></li>
		        
					<li><a href="/categories/随笔">随笔总结</a></li>
		        
					<li><a href="/categories/笔记">学习笔记</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
						<a class="github" target="_blank" href="http://springxiege.gitbub.io" title="github">github</a>
			        
						<a class="weibo" target="_blank" href="http://weibo.com/springxiegel" title="weibo">weibo</a>
			        
						<a class="mail" target="_blank" href="mailto://springxiege@sina.cn" title="mail">mail</a>
			        
				</div>
			</nav>
		</header>				
	</div>
</nav>

      <div class="body-wrap"><article id="post-JS实现倒计时" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2016/04/22/JS实现倒计时/" class="article-date">
  	<time datetime="2016-04-22T14:11:24.000Z" itemprop="datePublished">2016-04-22</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JS实现倒计时
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
	<div class="article-tag tagcloud">
		<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JS/">JS</a></li></ul>
	</div>

        
	<div class="article-category tagcloud">
	<a class="article-category-link" href="/categories/随笔/">随笔</a>
	</div>


        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>很久之前就写了一个时间插件，该插件是根据工作开发过程中的需要写的，关于时间插件，应该有不少人开发过，这里我写了一个个人觉得还算可以的方法吧，不过代码有待优化，一旦功能写好了，除非有bug，不然我也就懒得去优化了。我写了一个倒计时的对象setCountDown.<br><a id="more"></a><br>首先，我定义了一个倒计时对象，如下：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> setCountDown = &#123;&#125;;</div></pre></td></tr></table></figure></p>
<p>或者定义一个倒计时函数，如下：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">setCountDown</span>(<span class="params"></span>)</span>&#123;&#125;;</div></pre></td></tr></table></figure></p>
<p>上面两种方法都可以，也就是使用的方法不一样而已，我这里使用的是第一种方法，如果使用第二种方法，可以使用如下方法：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> times = <span class="keyword">new</span> setCountDown();</div><div class="line">times.prototype.init = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="comment">// code here....</span></div><div class="line">&#125;;</div><div class="line">times.prototype.getSecond = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="comment">// code here....</span></div><div class="line">&#125;;</div></pre></td></tr></table></figure></p>
<p>第二种方式我就不再多说了，主要说说我的方法。<br>首先，我给该对象添加了一个获取时间(单位为秒)的方法getSecond,如下：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">getSecond:<span class="function"><span class="keyword">function</span>:(<span class="params">times</span>)</span>&#123;</div><div class="line">    <span class="keyword">if</span>(times)&#123;</div><div class="line">        <span class="keyword">var</span> year = <span class="built_in">parseInt</span>(times.slice(<span class="number">0</span>,<span class="number">4</span>)),</div><div class="line">            month = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/-\d*/gi</span>)[<span class="number">0</span>].replace(<span class="string">'-'</span>,<span class="string">''</span>)<span class="number">-1</span>),</div><div class="line">            day = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/-\d*/gi</span>)[<span class="number">1</span>].replace(<span class="string">'-'</span>,<span class="string">''</span>)),</div><div class="line">            hour = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/\d*:/</span>)[<span class="number">0</span>].replace(<span class="string">':'</span>,<span class="string">''</span>)),</div><div class="line">            minute = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/:\d*/</span>)[<span class="number">0</span>].replace(<span class="string">':'</span>,<span class="string">''</span>));</div><div class="line">        <span class="keyword">return</span> (<span class="keyword">new</span> <span class="built_in">Date</span>(year,month,day,hour,minute,<span class="number">0</span>)).getTime()/<span class="number">1000</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> (<span class="keyword">new</span> <span class="built_in">Date</span>()).getTime()/<span class="number">1000</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>然后我根据获取的的时间秒，另外给对象添加了个获取天数、小时，分钟，秒的方法，如下：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">getCountdown:<span class="function"><span class="keyword">function</span>(<span class="params">time</span>)</span>&#123;</div><div class="line">    <span class="keyword">var</span> curShowTimeSecondsVal = <span class="keyword">this</span>.getSecond(time) - <span class="keyword">this</span>.getSecond();</div><div class="line">    <span class="keyword">if</span>(curShowTimeSecondsVal&lt;<span class="number">0</span>) <span class="keyword">return</span> [<span class="number">0</span>,<span class="string">'00'</span>,<span class="string">'00'</span>,<span class="string">'00'</span>];</div><div class="line">    <span class="comment">// console.log(curShowTimeSecondsVal)</span></div><div class="line">    <span class="comment">// 剩余秒数</span></div><div class="line">    <span class="keyword">var</span> curShowTimeSeconds = <span class="built_in">parseInt</span>(curShowTimeSecondsVal%<span class="number">60</span>);</div><div class="line">    <span class="comment">// 计算剩余天数</span></div><div class="line">    <span class="keyword">var</span> curShowTimeDays = <span class="built_in">parseInt</span>(curShowTimeSecondsVal/<span class="number">3600</span>/<span class="number">24</span>);</div><div class="line">    <span class="comment">// 计算剩余小时</span></div><div class="line">    <span class="keyword">var</span> curShowTimeHours = <span class="built_in">parseInt</span>((curShowTimeSecondsVal/<span class="number">3600</span>)) - curShowTimeDays*<span class="number">24</span>;</div><div class="line">    <span class="comment">// 计算剩余分钟</span></div><div class="line">    <span class="keyword">var</span> curShowTimeMinutes = <span class="built_in">parseInt</span>((curShowTimeSecondsVal - <span class="built_in">parseInt</span>((curShowTimeSecondsVal/<span class="number">3600</span>))*<span class="number">3600</span>)/<span class="number">60</span>);</div><div class="line">    curShowTimeHours = curShowTimeHours &gt; <span class="number">9</span> ? curShowTimeHours : <span class="string">'0'</span> + curShowTimeHours;</div><div class="line">    curShowTimeSeconds = curShowTimeSeconds &gt; <span class="number">9</span> ? curShowTimeSeconds : <span class="string">'0'</span> + curShowTimeSeconds;</div><div class="line">    curShowTimeMinutes = curShowTimeMinutes &gt; <span class="number">9</span> ? curShowTimeMinutes : <span class="string">'0'</span> + curShowTimeMinutes;</div><div class="line">    <span class="keyword">return</span> [curShowTimeDays,curShowTimeHours,curShowTimeMinutes,curShowTimeSeconds];</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>在根据上面的getCountdown获取时间后，根据返回的时间数组显示，接下来写了另外一个显示的方法，如下：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">setShowTime:<span class="function"><span class="keyword">function</span>(<span class="params">endtime,done,callback</span>)</span>&#123;</div><div class="line">    <span class="keyword">var</span> _this = <span class="keyword">this</span>;</div><div class="line">    <span class="comment">// var oSetTime = document.getElementById('time');</span></div><div class="line">    <span class="keyword">var</span> day = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">0</span>],</div><div class="line">        hour = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">1</span>],</div><div class="line">        minute = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">2</span>],</div><div class="line">        second = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">3</span>];</div><div class="line">    done([day,hour,minute,second])</div><div class="line">    <span class="comment">// oSetTime.innerHTML = '剩余时间：'+day+'天'+hour+'小时'+minute+'分'+second+'秒';</span></div><div class="line">    <span class="keyword">if</span>(day==<span class="number">0</span>&amp;&amp;hour==<span class="string">'00'</span>&amp;&amp;minute==<span class="string">'00'</span>&amp;&amp;second==<span class="string">'00'</span>)&#123;</div><div class="line">        clearInterval(_this.timer);</div><div class="line">        _this.timer =<span class="literal">null</span>;</div><div class="line">        <span class="keyword">if</span>(callback) callback();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>接下来添加一个入口函数，在入口函数设置相应的参数和相应的回调函数，还有一个操作函数，具体代码如下：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">init:<span class="function"><span class="keyword">function</span>(<span class="params">opt</span>)</span>&#123;</div><div class="line">    <span class="keyword">var</span> _this = <span class="keyword">this</span>;</div><div class="line">    <span class="keyword">this</span>.setShowTime(opt.endtime,opt.done);</div><div class="line">    <span class="keyword">this</span>.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        _this.setShowTime(opt.endtime,opt.done,opt.callback)</div><div class="line">    &#125;,<span class="number">1000</span>)</div><div class="line">&#125;,</div></pre></td></tr></table></figure></p>
<p>init函数中的opt的配置如下：</p>
<ul>
<li>endtime –&gt;结束时间</li>
<li>done –&gt; 操作函数，用于操作dom，动态显示时间</li>
<li>callback –&gt; 回调函数，用于当倒计时结束时的回调，作相应的操作<br>具体使用方法如下：<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">setCountDown.init(&#123;</div><div class="line">    endtime:<span class="string">'2016-7-8 00:00'</span>,</div><div class="line">    done:<span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>&#123;</div><div class="line">        <span class="comment">// console.log(data)</span></div><div class="line">        <span class="built_in">document</span>.getElementById(<span class="string">'time'</span>).innerHTML = <span class="string">'剩余时间：'</span>+data[<span class="number">0</span>]+<span class="string">'天'</span>+data[<span class="number">1</span>]+<span class="string">'小时'</span>+data[<span class="number">2</span>]+<span class="string">'分'</span>+data[<span class="number">3</span>]+<span class="string">'秒'</span>;</div><div class="line">    &#125;,</div><div class="line">    callback:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="comment">// window.location.reload()</span></div><div class="line">    &#125;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
</li>
</ul>
<p>最后，下面贴上倒计时的整个代码：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div></pre></td><td class="code"><pre><div class="line">    <span class="comment">/**</span></div><div class="line"><span class="comment">     * [setCountDown description]</span></div><div class="line"><span class="comment">     * @type &#123;Object&#125;</span></div><div class="line"><span class="comment">     * @Author xiege</span></div><div class="line"><span class="comment">     * @param init  入口函数</span></div><div class="line"><span class="comment">     * @param opt&#123;</span></div><div class="line"><span class="comment">     *              endtime:'',</span></div><div class="line"><span class="comment">     *              done:function()&#123;&#125;,</span></div><div class="line"><span class="comment">     *              callback:function()&#123;&#125;</span></div><div class="line"><span class="comment">     *           &#125;[endtime:结束时间,done:操作函数,callback:倒计时归零时回调函数]</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="keyword">var</span> setCountDown = &#123;</div><div class="line">            timer:<span class="literal">null</span>,</div><div class="line">            init:<span class="function"><span class="keyword">function</span>(<span class="params">opt</span>)</span>&#123;</div><div class="line">                <span class="keyword">var</span> _this = <span class="keyword">this</span>;</div><div class="line">                <span class="keyword">this</span>.setShowTime(opt.endtime,opt.done);</div><div class="line">                <span class="keyword">this</span>.timer = setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">                    _this.setShowTime(opt.endtime,opt.done,opt.callback)</div><div class="line">                &#125;,<span class="number">1000</span>)</div><div class="line">            &#125;,</div><div class="line">            getCountdown:<span class="function"><span class="keyword">function</span>(<span class="params">time</span>)</span>&#123;</div><div class="line">                <span class="keyword">var</span> curShowTimeSecondsVal = <span class="keyword">this</span>.getSecond(time) - <span class="keyword">this</span>.getSecond();</div><div class="line">                <span class="keyword">if</span>(curShowTimeSecondsVal&lt;<span class="number">0</span>) <span class="keyword">return</span> [<span class="number">0</span>,<span class="string">'00'</span>,<span class="string">'00'</span>,<span class="string">'00'</span>];</div><div class="line">                <span class="comment">// console.log(curShowTimeSecondsVal)</span></div><div class="line">                <span class="comment">// 剩余秒数</span></div><div class="line">                <span class="keyword">var</span> curShowTimeSeconds = <span class="built_in">parseInt</span>(curShowTimeSecondsVal%<span class="number">60</span>);</div><div class="line">                <span class="comment">// 计算剩余天数</span></div><div class="line">                <span class="keyword">var</span> curShowTimeDays = <span class="built_in">parseInt</span>(curShowTimeSecondsVal/<span class="number">3600</span>/<span class="number">24</span>);</div><div class="line">                <span class="comment">// 计算剩余小时</span></div><div class="line">                <span class="keyword">var</span> curShowTimeHours = <span class="built_in">parseInt</span>((curShowTimeSecondsVal/<span class="number">3600</span>)) - curShowTimeDays*<span class="number">24</span>;</div><div class="line">                <span class="comment">// 计算剩余分钟</span></div><div class="line">                <span class="keyword">var</span> curShowTimeMinutes = <span class="built_in">parseInt</span>((curShowTimeSecondsVal - <span class="built_in">parseInt</span>((curShowTimeSecondsVal/<span class="number">3600</span>))*<span class="number">3600</span>)/<span class="number">60</span>);</div><div class="line">                curShowTimeHours = curShowTimeHours &gt; <span class="number">9</span> ? curShowTimeHours : <span class="string">'0'</span> + curShowTimeHours;</div><div class="line">                curShowTimeSeconds = curShowTimeSeconds &gt; <span class="number">9</span> ? curShowTimeSeconds : <span class="string">'0'</span> + curShowTimeSeconds;</div><div class="line">                curShowTimeMinutes = curShowTimeMinutes &gt; <span class="number">9</span> ? curShowTimeMinutes : <span class="string">'0'</span> + curShowTimeMinutes;</div><div class="line">                <span class="keyword">return</span> [curShowTimeDays,curShowTimeHours,curShowTimeMinutes,curShowTimeSeconds];</div><div class="line">            &#125;,</div><div class="line">            getSecond:<span class="function"><span class="keyword">function</span>(<span class="params">times</span>)</span>&#123;</div><div class="line">                <span class="keyword">if</span>(times)&#123;</div><div class="line">                    <span class="keyword">var</span> year = <span class="built_in">parseInt</span>(times.slice(<span class="number">0</span>,<span class="number">4</span>)),</div><div class="line">                        month = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/-\d*/gi</span>)[<span class="number">0</span>].replace(<span class="string">'-'</span>,<span class="string">''</span>)<span class="number">-1</span>),</div><div class="line">                        day = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/-\d*/gi</span>)[<span class="number">1</span>].replace(<span class="string">'-'</span>,<span class="string">''</span>)),</div><div class="line">                        hour = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/\d*:/</span>)[<span class="number">0</span>].replace(<span class="string">':'</span>,<span class="string">''</span>)),</div><div class="line">                        minute = <span class="built_in">parseInt</span>(times.match(<span class="regexp">/:\d*/</span>)[<span class="number">0</span>].replace(<span class="string">':'</span>,<span class="string">''</span>));</div><div class="line">                    <span class="keyword">return</span> (<span class="keyword">new</span> <span class="built_in">Date</span>(year,month,day,hour,minute,<span class="number">0</span>)).getTime()/<span class="number">1000</span>;</div><div class="line">                &#125;</div><div class="line">                <span class="keyword">return</span> (<span class="keyword">new</span> <span class="built_in">Date</span>()).getTime()/<span class="number">1000</span>;</div><div class="line">            &#125;,</div><div class="line">            setShowTime:<span class="function"><span class="keyword">function</span>(<span class="params">endtime,done,callback</span>)</span>&#123;</div><div class="line">                <span class="keyword">var</span> _this = <span class="keyword">this</span>;</div><div class="line">                <span class="comment">// var oSetTime = document.getElementById('time');</span></div><div class="line">                <span class="keyword">var</span> day = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">0</span>],</div><div class="line">                    hour = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">1</span>],</div><div class="line">                    minute = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">2</span>],</div><div class="line">                    second = <span class="keyword">this</span>.getCountdown(endtime)[<span class="number">3</span>];</div><div class="line">                done([day,hour,minute,second])</div><div class="line">                <span class="comment">// oSetTime.innerHTML = '剩余时间：'+day+'天'+hour+'小时'+minute+'分'+second+'秒';</span></div><div class="line">                <span class="keyword">if</span>(day==<span class="number">0</span>&amp;&amp;hour==<span class="string">'00'</span>&amp;&amp;minute==<span class="string">'00'</span>&amp;&amp;second==<span class="string">'00'</span>)&#123;</div><div class="line">                    clearInterval(_this.timer);</div><div class="line">                    _this.timer = <span class="literal">null</span>;</div><div class="line">                    <span class="keyword">if</span>(callback) callback();</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        &#125;;</div><div class="line">    setCountDown.init(&#123;</div><div class="line">        endtime:start,</div><div class="line">        done:<span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>&#123;</div><div class="line">            <span class="comment">// console.log(data)</span></div><div class="line">            <span class="built_in">document</span>.getElementById(<span class="string">'time'</span>).innerHTML = <span class="string">'剩余时间：'</span>+data[<span class="number">0</span>]+<span class="string">'天'</span>+data[<span class="number">1</span>]+<span class="string">'小时'</span>+data[<span class="number">2</span>]+<span class="string">'分'</span>+data[<span class="number">3</span>]+<span class="string">'秒'</span>;</div><div class="line">        &#125;,</div><div class="line">        callback:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">            <span class="comment">// window.location.reload()</span></div><div class="line">            </div><div class="line">        &#125;</div><div class="line">    &#125;)</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>总结：写类似这种对象式的插件，一定要注意作用域。</p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/04/25/node-study-0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption"><</strong>
      <div class="article-nav-title">
        
          NodeJS学习笔记(一)从零开始
        
      </div>
    </a>
  
  
    <a href="/2016/04/22/HTML5的本地存储之localstorage/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">HTML5的本地存储之localstorage</div>
      <strong class="article-nav-caption">></strong>
    </a>
  
</nav>

  
</article>


<div class="share share_jia">
	<!-- JiaThis Button BEGIN -->
	<div class="jiathis_style">
		<span class="jiathis_txt">分享到: &nbsp; </span>
		<a class="jiathis_button_facebook"></a> 
    <a class="jiathis_button_twitter"></a>
    <a class="jiathis_button_plus"></a> 
    <a class="jiathis_button_tsina"></a>
		<a class="jiathis_button_cqq"></a>
		<a class="jiathis_button_douban"></a>
		<a class="jiathis_button_weixin"></a>
		<a class="jiathis_button_tumblr"></a>
    <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	</div>
	<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=1405949716054953" charset="utf-8"></script>
	<!-- JiaThis Button END -->
</div>






<div class="duoshuo">
	<!-- 多说评论框 start -->
	<div class="ds-thread" data-thread-key="JS实现倒计时" data-title="JS实现倒计时" data-url="http://yoursite.com/2016/04/22/JS实现倒计时/"></div>
	<!-- 多说评论框 end -->
	<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"true"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
	<!-- 多说公共JS代码 end -->
</div>




</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
    	<div class="footer-left">
    		&copy; 2017 心情
    	</div>
      	<div class="footer-right">
      		<a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/litten/hexo-theme-yilia" target="_blank">Yilia</a> by Litten
      	</div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script>
<script src="/js/main.js"></script>






<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  </div>
</body>
</html>